


/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：03/01/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/  
package com.application.yfsyslog.dao;
import java.util.List;
import java.util.ArrayList;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import com.manager.commons.page.PageBean;
import org.apache.log4j.Logger;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;
import com.application.yfsyslog.entity.YfSyslog;
import com.application.yfsyslog.struts.form.YfSyslogForm;
 /**
 * 
 * Description:YfSyslogDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 03/01/2012 
 * 
 */
public class YfSyslogDAO extends HibernateGenericDao<YfSyslog>
{
    private static final Logger logger = Logger.getLogger(YfSyslogDAO.class);

	private YfSyslog yfSyslog;

	public YfSyslog getYfSyslog() {
		return this.yfSyslog;
	}

	public void setYfSyslog(YfSyslog yfSyslog) {
		this.yfSyslog = yfSyslog;
	}

	public Integer getAllCountYfSyslog(YfSyslogForm yfSyslogForm){
	    List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT COUNT(0) FROM yf_syslog  t left join usertable u on t.user_id = u.id  WHERE 1=1 ");
                sql = getStringBuffer(yfSyslogForm,sql,args);
		return super.getCount(sql.toString(),args.toArray());
	}
        
	public void deleteYfSyslogs(YfSyslogForm yfSyslogForm){    	
    		String hql = " delete from YfSyslog where id in  ( " + yfSyslogForm.getIds() + ")";
		Session session = this.getSession();
		try
		{
			session.createQuery(hql).executeUpdate();
		}
		catch(RuntimeException re)
		{
		        logger.error("deleteYfSyslogs failed");	
			throw re;
		}
		finally
		{
			if (session != null)
			{
				releaseSession(session);
			}
		}
       }

	public List searchYfSyslog(YfSyslogForm yfSyslogForm,PageBean pageBean){  
	    List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT * FROM yf_syslog  t left join usertable u on t.user_id = u.id  WHERE 1=1 ");
		sql = getStringBuffer(yfSyslogForm,sql,args);
		return getPageInfo(pageBean,sql.toString(),args);
	}

	/**
	 * 查询条件的封装 
	 * @param departmentForm 
	 * @return
	 */
	private StringBuffer getStringBuffer(YfSyslogForm yfSyslogForm,StringBuffer buf,List args){
		/* 需要加入查询条件时封装
		*/
		
        if(StringUtils.isNotBlank(yfSyslogForm.getUserId())){
		    buf.append(" and t.USER_ID like ? ");
			args.add("%" + yfSyslogForm.getUserId().trim() + "%");
		}
        if(StringUtils.isNotBlank(yfSyslogForm.getOrg())){
		    buf.append(" and t.org like ? ");
			args.add("%" + yfSyslogForm.getOrg().trim() + "%");
		}
		if(StringUtils.isNotBlank(yfSyslogForm.getRname())){
		    buf.append(" and u.rname like ? ");
		    args.add("%" + yfSyslogForm.getRname().trim() + "%");
		}
		if(yfSyslogForm.getStartDate()!=null){
			buf.append(" and t.login_time > ? ");
			args.add(yfSyslogForm.getStartDate());
		}
		if(yfSyslogForm.getEndDate()!=null){
			buf.append(" and t.login_time < ? ");
			args.add(yfSyslogForm.getEndDate());
		}
		buf.append(" order by t.login_time desc ");
        return buf;
	}
	
}
	